package email

import (
	"errors"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"net/url"
	"vrcm/src/vrcm/app/admin/models"
	"vrcm/src/vrcm/app/admin/service/dto/email"
	"vrcm/src/vrcm/common/service"
	ext "vrcm/src/vrcm/envconfig"
)

var client = &http.Client{}

type EmailService struct {
	service.Service
}

// SendEmailCode 发送短信验证码
func (s *EmailService) SendEmailCode(d *email.SendEmailCodeReq) error {

	params := url.Values{}
	urlString := ext.ExtConfig.ServiceConfig.VrmServiceUrl + "/api/v1/email/sendEmailCode"
	//urlString := "http://127.0.0.1:8084" + "/api/v1/email/sendEmailCode"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	params.Set("email", d.Email)
	params.Set("permissionToken", "vsnb")
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	s.Log.Trace(urlPath)
	// 获取 request请求
	request, err := http.NewRequest("POST", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	//request.Header.Add("Content-Type", "application/json")
	if err != nil {
		s.Log.Errorf("GetHttpSkip Request Error:", err)
		return err
	}
	response, err := client.Do(request)
	if err != nil {
		s.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		s.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	result := gjson.Parse(string(body))
	s.Log.Trace(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}

	return nil
}

// CheckEmailCode 校验短信验证码
func (s *EmailService) CheckEmailCode(d *email.CheckEmailCodeReq) error {

	params := url.Values{}
	urlString := ext.ExtConfig.ServiceConfig.VrmServiceUrl + "/api/v1/email/checkEmailCode"
	//urlString := "http://127.0.0.1:8084" + "/api/v1/email/checkEmailCode"
	Url, err := url.Parse(urlString)
	if err != nil {
		return err
	}
	params.Set("tenantId", d.TenantId)
	params.Set("email", d.Email)
	params.Set("code", d.Code)
	params.Set("permissionToken", "vsnb")
	Url.RawQuery = params.Encode()
	urlPath := Url.String()

	s.Log.Trace(urlPath)
	// 获取 request请求
	request, err := http.NewRequest("POST", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	//request.Header.Add("Content-Type", "application/json")
	if err != nil {
		s.Log.Errorf("GetHttpSkip Request Error:", err)
		return err
	}
	response, err := client.Do(request)
	if err != nil {
		s.Log.Errorf("GetHttpSkip Response Error:", err)
		return err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		s.Log.Errorf("读取响应体失败 error:%s", err)
		return err
	}
	result := gjson.Parse(string(body))

	if result.Get("code").Int() != http.StatusOK {
		return errors.New(result.Get("message").String())
	}

	return nil
}
